package com.topnav.mc.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.js.lib.annotation.ApiLogger;
import com.js.lib.result.Result;
import com.topnav.mc.uitls.QueryPage;
import com.topnav.mc.validator.group.ISelect;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * view 查询
 * @param <T>
 * @param <Service>
 */
public abstract class ViewQueryController<T,Service extends IService<T>> {
    @Autowired(required = false)
    protected Service baseService;

    //@ApiLogger(apiName="/query/list")
    @GetMapping("/query/view/list")
    @ApiOperation(value = "查询所有")
    @ResponseBody
    public Result queryViewList() {
        List datas = baseService.list();
        return Result.ok(datas);
    }

    /**
     * 通过查询条件获取多条数据
     */
    //@ApiLogger(apiName="/query/view/wherelist")
    @ApiOperation(value = "通过查询条件获取多条数据")
    @PostMapping("/query/view/wherelist")
    @ResponseBody
    public Result queryViewWhereList(@Validated(ISelect.class) @RequestBody T data) {
        List datas = baseService.list(new QueryWrapper<>(data));
        return Result.ok(datas);
    }

    /**
     * 分页查询
     * @param p
     * @return
     */
    //@ApiLogger(apiName="/query/view/page")
    @ApiOperation(value = "带有条件的分页查询")
    @PostMapping("/query/view/page")
    @ResponseBody
    public Result queryViewPage(@Validated(IPage.class) @RequestBody QueryPage<T> p) {

        Page datas = baseService.page(p.getPage(), new QueryWrapper<>(p.getWhere()));
        return Result.ok(datas);
    }
}
